package netTrackerGUI;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
import netTracker.Cell;
import netTracker.Util;

/**
 * Represents the GUI components of a cell object.
 * 
 * @author David Cummings
 */

@SuppressWarnings("serial")	// GUI does not need to be serialized

public class CellGUI extends JPanel implements MouseListener{
	/** Utility class with debug flag: */
	Util util = new Util(this, true);
	
	/** The NetTrackerGUI that contains this cell. */
	NetTrackerGUI netTrackerGUI;
	/** The cell represented by the GUI. */
	Cell cell;
	/** The colors used by the GUI to represent the cell. */
	Color outlinedColor, selectedColor, highlightedColor, backgroundColor, textColor;
	/** Rectangle that represent the border and the outline of the cell. */
	Rectangle cellBorder, outline;
	/** x,y coordinates to represent cell location within a pattern. */
	int cellX, cellY;
	
	/** 
	 * Constructs a Cell GUI with default color scheme.
	 * @param myCell the cell represented by this GUI.
	 */
	public CellGUI(Cell cell, NetTrackerGUI netTrackerGUI, int cellX, int cellY){
		this.cell = cell;
		this.netTrackerGUI = netTrackerGUI;
		this.cellX=cellX;
		this.cellY=cellY; 
		Color[] defaultColorScheme = {Color.GREEN, Color.BLUE, Color.LIGHT_GRAY, Color.BLACK, Color.RED};
		setColorScheme(defaultColorScheme);
		addMouseListener(this);
	}
	
	/**
	 * Defines the color scheme of a cell.
	 * Index 0: Outlined Color
	 * Index 1: Selected Color
	 * Index 2: Background Color
	 * Index 3: Text Color
	 * Index 4: Highlighted Color
	 * @param colorScheme an array of colors to assign to various color components. 
	 *  
	 */
	public void setColorScheme(Color[] colorScheme){
		outlinedColor = colorScheme[0];
		selectedColor = colorScheme[1];
		backgroundColor = colorScheme[2];
		textColor = colorScheme[3];
		highlightedColor = colorScheme[4];
	}

	/**
	 * @return the cell represented by the GUI
	 */
	public Cell getCell() {
		return cell;
	}

	/**
	 * @param cell the cell to connect to the GUI
	 */
	public void setCell(Cell cell) {
		this.cell = cell;
	}

	public void setNoteName(String newName){
		cell.setNoteName(newName);
		repaint();
	}

	/**
	 * @return the rectangle representing the cell border
	 */
	public Rectangle getCellBorder() {
		return cellBorder;
	}

	/**
	 * @return the rectangle representing the outline
	 */
	public Rectangle getOutline() {
		return outline;
	}
	
	/**
	 * @return the x coordinate of this cell.
	 */
	public int getCellX(){
		return cellX;
	}
	/**
	 * @return the y coordinate of this cell.
	 */
	public int getCellY(){
		return cellY;
	}
	
	/**
	 * Selects the cell and repaints to update.
	 */
	public void select(){
		cell.setSelected(true);
		repaint();
	}
	/**
	 * Deselects the cell and repaints to update.
	 */
	public void deselect(){
		cell.setSelected(false);
		repaint();
	}
	/**
	 * Outlines the cell with a specific ID and repaints to update.
	 */
	public void outline(int outlineID){
		cell.setOutlined(outlineID, true);
		repaint();
	}
	/**
	 * Unoutlines the cell and repaints to update.
	 * @param outlineID 
	 */
	public void unoutline(int outlineID){
		cell.setOutlined(outlineID, false);
		repaint();
	}
	
	/**
	 * Paints a rectangle to represent the cell.
	 */
	public void paintComponent(Graphics g){
		Graphics2D g2 = (Graphics2D)g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		/** Draw a rectangle to represent the cell: */
		Rectangle2D myRectangle = new Rectangle2D.Double(0,0,
				NetTrackerGUI.CELLWIDTH-1,NetTrackerGUI.CELLHEIGHT);
		
		/** Fill the cell with an appropriate background color. */
		if (cell.isSelected())
			g2.setPaint(selectedColor);
		else if(cell.isHighlighted())
			g2.setPaint(highlightedColor);
		else
			g2.setPaint(backgroundColor);
		g2.fill(myRectangle);
		
		/** Draw the rectangle. */
		g2.setPaint(Color.BLACK);
		g2.draw(myRectangle); 
		
		/** Draw another, smaller rectangle if the cell is outlined. */
		if (cell.isOutlined()){
			myRectangle.setRect(1, 1, NetTrackerGUI.CELLWIDTH-3, NetTrackerGUI.CELLHEIGHT-2);
			g2.setPaint(outlinedColor);
			g2.draw(myRectangle);
		}
		
		/** Draw the note name: */
		g2.setPaint(textColor);
		g2.setFont(NetTrackerGUI.NOTE_FONT);
		// TODO: change these values to be relative and centered:
		g2.drawString(cell.getNoteName(), 3, 12); 		
	}
	
	/** Override size methods to ensure a correct cell size: */
    public Dimension getMinimumSize()
    { return new Dimension(NetTrackerGUI.CELLWIDTH,NetTrackerGUI.CELLHEIGHT); }
    public Dimension getMaximumSize()
    { return getMinimumSize(); }
    public Dimension getPreferredSize()
    { return getMinimumSize(); }
    
	/** 
	 * The following methods handle mouse listener events.
	 */
	public void mouseClicked(MouseEvent arg0) {
	}
	
	public void mouseEntered(MouseEvent arg0) {	
	}
	
	public void mouseExited(MouseEvent arg0) {	
	}
	
	public void mousePressed(MouseEvent e) {	
		netTrackerGUI.getTrackerPanel().requestFocusInWindow();
		if(e.getButton() == MouseEvent.BUTTON1){ // LEFT CLICK
			netTrackerGUI.setPressed(true);
			/** Spawn a cell selector thread to try to select this cell:  */
			new Thread(new CellSelectorThread(netTrackerGUI.getTrackerPanel(), this)).start();
		}
	}
	
	public void mouseReleased(MouseEvent arg0) {
		netTrackerGUI.setPressed(false);
	}
	
	public String toString(){
		return (cellX+","+cellY);
	}
}
